<template>
	<view class="container">
		<uni-forms ref="form" :model="formData" :rules="rules" label-position="top">
			<!-- 客户姓名 -->
			<uni-forms-item label="客户姓名" name="customerName" required>
				<uni-easyinput 
					v-model="formData.customerName" 
					placeholder="请输入客户姓名"
					trim="both"
				/>
			</uni-forms-item>
			
			<!-- 账单日期 -->
			<uni-forms-item label="账单日期" name="billDate" required>
				<uni-datetime-picker 
					type="date" 
					v-model="formData.billDate"
					:end="today"
					return-type="string"
				/>
			</uni-forms-item>
			
			<!-- 金额 -->
			<uni-forms-item label="金额" name="amount" required>
				<view class="amount-input">
					<text class="prefix">¥</text>
					<uni-easyinput 
						v-model="formData.amount" 
						type="number"
						placeholder="请输入金额"
					/>
				</view>
			</uni-forms-item>
			
			<!-- 支付状态 -->
			<uni-forms-item label="支付状态" name="status" required>
				<view class="status-box">
					<view 
						v-for="item in payStatusOptions" 
						:key="item.value"
						class="status-item"
						:class="{ active: formData.status === item.value }"
						@click="formData.status = item.value"
					>
						{{ item.text }}
					</view>
				</view>
			</uni-forms-item>
			
			<!-- 备注 -->
			<uni-forms-item label="备注" name="remarks">
				<uni-easyinput 
					type="textarea" 
					v-model="formData.remarks" 
					placeholder="请输入备注信息(选填)"
					maxlength="200"
					:autoHeight="true"
				/>
			</uni-forms-item>
		</uni-forms>
		
		<view class="btn-submit">
			<button type="primary" @click="submitForm">保存账单</button>
		</view>
	</view>
</template>

<script>
import { saveBill } from '@/api/manage/bill'

export default {
	data() {
		return {
			formData: {
				customerName: '',
				billDate: this.formatDate(new Date()),
				amount: '',
				status: '0',
				remarks: ''
			},
			rules: {
				customerName: {
					rules: [{
						required: true,
						errorMessage: '请输入客户姓名'
					}]
				},
				billDate: {
					rules: [{
						required: true,
						errorMessage: '请选择账单日期'
					}]
				},
				amount: {
					rules: [{
						required: true,
						errorMessage: '请输入金额'
					}, {
						validateFunction: function(rule, value, data, callback) {
							if (value <= 0) {
								callback('金额必须大于0')
							}
							return true
						}
					}]
				},
				status: {
					rules: [{
						required: true,
						errorMessage: '请选择支付状态'
					}]
				}
			},
			payStatusOptions: [{
				text: '未支付',
				value: '0'
			}, {
				text: '已支付',
				value: '1'
			}]
		}
	},
	computed: {
		today() {
			return this.formatDate(new Date())
		}
	},
	methods: {
		formatDate(date) {
			const year = date.getFullYear()
			const month = String(date.getMonth() + 1).padStart(2, '0')
			const day = String(date.getDate()).padStart(2, '0')
			return `${year}-${month}-${day}`
		},
		submitForm() {
			this.$refs.form.validate().then(res => {
				// 验证通过
				saveBill(this.formData).then(response => {
					uni.showToast({
						title: '保存成功',
						icon: 'success'
					})
					// 重置表单
					this.resetForm()
				}).catch(error => {
					uni.showToast({
						title: '保存失败',
						icon: 'error'
					})
				})
			}).catch(errors => {
				console.log('表单错误:', errors)
			})
		},
		resetForm() {
			this.formData = {
				customerName: '',
				billDate: this.formatDate(new Date()),
				amount: '',
				status: '0',
				remarks: ''
			}
			this.$refs.form.clearValidate()
		}
	}
}
</script>

<style lang="scss">
.container {
	padding: 30rpx;
	background-color: #f5f5f5;
	min-height: 100vh;
	
	.uni-forms {
		background-color: #fff;
		padding: 30rpx;
		border-radius: 12rpx;
	}
	
	.amount-input {
		display: flex;
		align-items: center;
		
		.prefix {
			font-size: 32rpx;
			color: #333;
			margin-right: 10rpx;
		}
	}
	
	.status-box {
		display: flex;
		gap: 20rpx;
		
		.status-item {
			flex: 1;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			border: 1px solid #dcdfe6;
			border-radius: 8rpx;
			color: #606266;
			
			&.active {
				background-color: #ecf5ff;
				border-color: #409eff;
				color: #409eff;
			}
		}
	}
	
	.btn-submit {
		margin-top: 50rpx;
		padding: 0 30rpx;
		
		button {
			width: 100%;
			height: 88rpx;
			line-height: 88rpx;
			border-radius: 44rpx;
		}
	}
}
</style>
